package com.jwb.pub.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jwb.pub.dao.BaseDAO;
import com.jwb.pub.service.DeptService;
import com.jwb.util.ToolsUtil;
@Service
public class DeptServiceImpl implements DeptService
{
	@Autowired
	private BaseDAO baseDAO;
	
	
	public JSONArray loadTree(String parentId,String removeChild,String showAll) {
		JSONArray array = new JSONArray();
		return queryTreeParent(parentId,array,removeChild, showAll);
	}


	public JSONArray queryTreeParent(String parentId,JSONArray array,String removeChild,String showAll){
		String treeLevelPro="";
		try {
			 treeLevelPro = ToolsUtil.getProp("config.properties", "treeLevel");
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		JSONObject obj = new JSONObject();
		JSONObject jsobject = new JSONObject();		
		String sql="select * from dept where dept_parent_id="+parentId+" and dept_sts='A'  order by dept_id";	
		try {
			jsobject=baseDAO.findBySqlToJosn(sql);
			if ("0".equals(jsobject.getString("respCode"))) {
				JSONArray list = jsobject.getJSONArray("list");
				for (int i = 0; i < list.size(); i++) {
					obj = new JSONObject();
					int deptcount=getDeptCount(String.valueOf(list.getJSONObject(i).getInteger("dept_id")), treeLevelPro);
					if("".equals(showAll))
					{
		    			deptcount=1;
					}
		    		
					if(deptcount>0)
		    		{	
						        obj.put("value", list.getJSONObject(i).getInteger("dept_id"));
						        obj.put("id", list.getJSONObject(i).getInteger("dept_id"));
								//if( list.getJSONObject(i).getString("dept_id").equals("508")|| list.getJSONObject(i).getString("dept_id").equals("510"))
								//{
								//   System.out.println( list.getJSONObject(i).getString("dept_id"));
								//}
								obj.put("pId", list.getJSONObject(i).getInteger("dept_parent_id"));
								obj.put("label", list.getJSONObject(i).getString("dept_name"));
								JSONArray jsonarray=queryTreeParent(String.valueOf(list.getJSONObject(i).getInteger("dept_id")),new JSONArray(),removeChild, showAll);
								
								if(!"".equals(removeChild))
								{
									if(jsonarray.size()>0)
									{
										obj.put("children", jsonarray);
									}
								}
								else
								{
									obj.put("children", jsonarray);
								}
								
							
								//obj.put("children", queryTreeParent(list.getJSONObject(i).getString("dept_id"),new JSONArray()));
								obj.put("deptId", list.getJSONObject(i).getInteger("dept_id"));					
								obj.put("deptName", list.getJSONObject(i).getString("dept_name"));
								obj.put("deptParentId", list.getJSONObject(i).getInteger("dept_parent_id"));
								obj.put("deptLevel", list.getJSONObject(i).getInteger("dept_level"));
							
								array.add(obj);
		    		}
					
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		return array;
	}

	
	
	public JSONObject getButtonInfo(String name,boolean right) throws Exception{
		JSONObject jsobject = new JSONObject();
		jsobject.put("name", name);
		if(right){
			jsobject.put("show", "1");
		}else{
			jsobject.put("show", "0");
		}		
		return jsobject;
	}

    /**
     * {"list":[{"id":"1","pid":"","name":"天津联通"},{"id":"2","pid":"","name":"塘沽区"},"respCode":"0","respDesc":"数据获取成功"]
     */
	@Override
	public JSONObject getDeptListHead(String id) throws Exception {
		// TODO Auto-generated method stub
		JSONObject jsobject = new JSONObject();
		String sql="select m.dept_id id,m.dept_parent_id pid,m.dept_name name  from (select * from DEPT where dept_sts='A') m start with m.dept_id='"
				+id+"' connect by prior m.dept_parent_id=m.dept_id" 
				+" union select m.dept_id id,m.dept_parent_id pid,m.dept_name name  from DEPT m where m.dept_sts='union'";
	
		jsobject=baseDAO.findBySqlToJosn(sql);
		return jsobject;
	}

    /** id=0
     * list":[{"id":"4","pid":"","name":"塘沽区1"},{"id":"5","pid":"","name":"塘沽区2","type":"0"},{"id":"5","pid":"","name":"塘沽区2","type":"0"},{"id":"6","pid":"","name":"塘沽区人员1","type":"1"},{"id":"6","pid":"","name":"塘沽区人员1","type":"1"}],"respCode":"0","respDesc":"数据获取成功"
     */
	@Override
	public JSONObject getDeptuserlist(String id) throws Exception {
		JSONObject jsobject = new JSONObject();
		String sql="select user_id id, real_name name, cur_dept_id  pid, '1' type "+
				"	  from user_info "+
				"	 where cur_dept_id = '"+id+"' and sts='A' "+
				"	union all "+
				"	select dept_id id, dept_name name, dept_parent_id pid ,'0' type "+
				"	  from dept "+
				"	 where dept_parent_id = '"+id+"' and dept_sts='A' ";
	
		jsobject=baseDAO.findBySqlToJosn(sql);
		return jsobject;
	}


	@Override
	public JSONObject getDeptConfigList(String deptId) throws Exception {
		// TODO Auto-generated method stub
		JSONObject jsobject = new JSONObject();
		String sql = "SELECT * from dept_config ";
		if(!"".equals(deptId)){
			sql +=" where dept_id = '"+deptId+"'";  
		}
	
		jsobject=baseDAO.findBySqlToJosn(sql);
		return jsobject;
	}


	@Override
	public JSONObject getDeptInfo(String deptId) {
		JSONObject jsobject = new JSONObject();
		String sql = "SELECT * from dept where 1=1 and dept_sts='A' ";
		if(!"".equals(deptId)){
			sql +=" and  dept_id = '"+deptId+"'  ";  
		}
		try{
			jsobject=baseDAO.findBySqlToJosn(sql);
		}catch(Exception e){
			e.printStackTrace();
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "数据访问失败");
		}
		return jsobject;
	}
	
	
	
	public int getDeptCount(String deptId,String deptLevel){
		String sql="";
		int count=0;
		sql = "SELECT count(*) FROM dept WHERE dept_id="+deptId+" and dept_sts='A' AND dept_level="+deptLevel;
		try{
			count=baseDAO.findBySqlCnt(sql);
			count +=deptCount(deptId,deptLevel);		
			
		} catch(Exception e){
			e.printStackTrace();
		}
		return count;
	}
	
	public int deptCount(String deptId,String deptLevel) throws Exception{
		String sql="";
		int count=0;
		sql = "SELECT dept_id, dept_level FROM dept WHERE dept_parent_id="+deptId+" and dept_sts='A' AND dept_level ="+deptLevel;
		JSONArray array = baseDAO.findBySqlToJosn(sql).getJSONArray("list");
		if(array.size()>0){
			count +=array.size();
			for(int i=0;i<array.size();i++){
				String id = String.valueOf(array.getJSONObject(i).getInteger("dept_id"));
				count+=deptCount(id, deptLevel);
			}			
		}
		return count;
	}
	
	
}
